/*
*       A simple 2D game map editor
*
*       This library is free software; you can redistribute it and/or
*       modify it under the terms of the GNU Lesser General Public
*       License as published by the Free Software Foundation; either
*       version 2.1 of the License, or (at your option) any later version.
*/
// Name        : tilewidget
// File        : tilewidget.cpp
// Description :
// Author      : hoha
// Date        : 01/07/2009
// Evolutions  : 01/07/2009, Version V0, Creation
#include "widget_tileset.h"
#include "common.h"
#include <QPalette>

int selected_tileId;

Wt_TileSet::Wt_TileSet(QWidget *parent)
: QWidget(parent)
{
    this->setAutoFillBackground(true);

QPalette palette;
palette.setColor(QPalette::Background, QColor(192,253,123));
//palette.setBrush(QPalette::Background, QBrush(QPixmap(":/background.png")));
this->setPalette(palette);

        this->set_widget_size();
        this->init_tileRender();
        this->init_render_tileRange();
        selected_tileId =NS_SELECTED_TILE;
}

Wt_TileSet::~Wt_TileSet()
{

}

void Wt_TileSet::set_widget_size()
{
    this->setMinimumSize(180,(MAX_TILE_PER_TILESET/2)*TILE_SPACE_HEIGHT);
    this->setMaximumSize(180,MAX_TILE_PER_TILESET*TILE_SPACE_HEIGHT);
}

void Wt_TileSet::init_tileRender()
{
        for(int i=0; i < MAX_TILE_PER_TILESET; i++)
        {
                int hNum = i%2;
                int vNum = i/2;
                tileRender[i].picPosX = hNum*TILE_SPACE_WIDTH + START_MARGINX;
                tileRender[i].picPosY = vNum*TILE_SPACE_HEIGHT + START_MARGINY;
                tileRender[i].txtPosX = tileRender[i].picPosX+30;
                tileRender[i].txtPosY = tileRender[i].picPosY+42;
        }
}

void Wt_TileSet::init_render_tileRange()
{
        render_beginTile = 0;
        render_endTile = RENDER_RANGE;  
}

//slot
void Wt_TileSet::update_render_tileRange(int scrollbarValue)
{
        if(scrollbarValue < (START_MARGINX + TILE_SPACE_HEIGHT))
        {
                render_beginTile = 0;
        }
        else
        {
                render_beginTile = this->cac_tileId(START_MARGINX+1, scrollbarValue);
                render_beginTile --;     
        }
        int tmpId = render_beginTile + RENDER_RANGE;
        render_endTile = (tmpId < MAX_TILE_PER_TILESET)?tmpId:MAX_TILE_PER_TILESET;
        update();
}

void Wt_TileSet::update_selected_tileId(int screenX, int screenY)
{
        if(screenX< START_MARGINX||screenY<START_MARGINY)
        {
                selected_tileId = NS_SELECTED_TILE;
        }
        else
        {
                selected_tileId = this->cac_tileId(screenX, screenY);
        }	
        update();
}

int Wt_TileSet::cac_tileId(int screenX, int screenY)
{
        int vNum = (screenY - START_MARGINY)/TILE_SPACE_HEIGHT;
        int hNum = (screenX - START_MARGINX)/TILE_SPACE_WIDTH;
        return (vNum*2 + hNum);
}

void Wt_TileSet::mousePressEvent(QMouseEvent *event)
{
        this->update_selected_tileId(event->x(), event->y());
}

void Wt_TileSet::draw_tiles(QPainter *painter)
{
        for(int i = render_beginTile; i < render_endTile; i++)
        {
                if(tileSet[i].tileInfo.isValid)
                {
                        painter->drawPixmap(tileRender[i].picPosX, tileRender[i].picPosY, *(tileSet[i].tileRender.tilePic), 0,0, tileSet[i].tileInfo.width, tileSet[i].tileInfo.height );
                }
                else
                {

                }
                painter->drawText(tileRender[i].txtPosX, tileRender[i].txtPosY, QString::number(i));
        }
}

void Wt_TileSet::mark_selected_tile(QPainter *painter)
{

    if(this->check_selected_tileId_valid())
    {
        painter->drawRect(tileRender[selected_tileId].picPosX, tileRender[selected_tileId].picPosY, \
                         TILE_SPACE_WIDTH, TILE_SPACE_HEIGHT);
    }
}

bool Wt_TileSet::check_selected_tileId_valid()
{
    if( (selected_tileId >= 0)&&(selected_tileId < MAX_TILE_PER_TILESET))
    {
        return true;
    }
    else
    {
        return false;
    }

}

void Wt_TileSet::paintEvent(QPaintEvent *event)
{
        QPainter painter(this);
        this->draw_tiles(&painter);
        this->mark_selected_tile(&painter);
}
